<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <template v-if="row.status==2">
        <el-button @click="to_edit(row,0)" size="small">无效</el-button>
        <el-button @click="to_edit(row,1)" size="small">有效</el-button>
      </template>
      <p v-else>无</p>
    </template>
  </list_url>
</template>
<script>
import {ElMessageBox} from "element-plus";
import {_href} from "@/js/s94_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if(in_right_admin('LogScore/add')) btns['添加'] = function(){ iframe('log_score/edit') }
    if(in_right_admin('LogScore/export')) btns['导出'] = function(){
      let param = s94Mjs.param_encode(app.$refs['list_url'].$refs['forms'].form_data());
      window.open(_href("/admin/log_score/export?"+param), '_blank')
    }
    let type_map = '完成任务|system,后台添加|admin,签到|sign,补签|repair_sign,兑换|exchange';
    let forms_config = {
      title_info: {label:'关键词',name:'where[title|info][like]',type:'text_one',value:''},
      phone: {label:'用户手机号',name:'where[user.phone][eq]',type:'text_one',value:''},
      type: {label:'类型',name:'where[type][eq]',type:'select_one',config:type_map,value:''},
      create_time: {label:'获取时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      head_img: {name:'head_img', label:'头像', width:'60px', type:'file_img'},
      nickname: {name:'nickname', label:'用户昵称', width:'120px'},
      phone: {name:'phone', label:'电话', width:'120px'},
      score_level: {name:'score_level', label:'积分段位', width:'80px', type:'select_one',config:[]},
      type: {name:'type', label:'类型', width:'80px', type:'select_one',config:type_map},
      title: {name:'title', label:'标题', min_width:'120px'},
      info: {name:'info', label:'详情', min_width:'120px'},
      change: {name:'change', label:'积分变动', width:'80px'},
      status: {name:'status', label:'状态', width:'80px', type:'select_one',config:["无效","有效","待审核"]},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'120px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/log_score/'+(is_del?'recycle':'list'),
        title: "积分记录列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row, ac){
      let can_edit = in_right_admin('LogScore/edit') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      if (!can_edit) return this.tips("权限不足！", 3);
      let status = ac ? 1 : 0;
      ElMessageBox.prompt('输入判定'+(['无效','有效'][status])+'的意见', '审核积分', {
        inputType: 'textarea',
        confirmButtonText: '提交',
        cancelButtonText: '取消',
        inputValidator: function (v){
          if (status==0 && !v) return '无效意见不能为空';
          // if (!v) return '备注必填';
          return true;
        },
      }).then((data)=>{
        let url = `/admin/log_score/edit?id=${row.id}`;
        ajax({url:url, method:'POST', data:{notes:data.value, status:status}}, (res)=>{
          this.tips("操作成功", 1);
          row.status = status;
          return false;
        });
      }).catch(() => {})
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/user_score_level/list?order[id]=asc',cache:true}, (res)=>{
      app.config.list_cols.score_level.config = this.decode_config(res.data.list, {content_key:'title', value_key:'level'});
      return false;
    })
  }
}
</script>
